/**
 * 哈希函数
 * @param key 转换的key
 * @param max 数组的长度（最大值）
 * @returns 返回索引值
 */
function hashFunc(key: string, max: number): number {
  let hashCode = 0;
  const length = key.length;
  for (let i = 0; i < length; i++) {
    // 霍纳法则计算hashcode
    hashCode = 31 * hashCode + key.charCodeAt(i);
  }
  // 区域得出索引
  const index = hashCode % max;

  return index;
}

console.log(hashFunc("abc", 7));
console.log(hashFunc("def", 7));
console.log(hashFunc("ghij", 7));
console.log(hashFunc("kfmn", 7));

// 越到后面越容易重复
console.log(hashFunc("opq", 7));
console.log(hashFunc("qod", 7));
console.log(hashFunc("asdqw", 7));
